package com.wfs.myapp.controller;

import com.wfs.myapp.common.ApiResult;
import com.wfs.myapp.common.Constant;
import com.wfs.myapp.entity.dto.req.user.LoginReqDTO;
import com.wfs.myapp.entity.dto.req.user.UserReqDto;
import com.wfs.myapp.service.UserService;
import com.wfs.myapp.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/user")
@Api(value = "用户", tags = "用户")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ApiResult<?> login(@Valid @RequestBody LoginReqDTO loginReqDTO) {
        return userService.login(loginReqDTO);
    }

    @GetMapping("/myGroupMemberList")
    @ApiOperation(value = "组员列表")
    public ApiResult<?> myGroupMemberList(HttpServletRequest request) {
        Long userId = JwtUtil.getUserId(request.getHeader(Constant.AUTHORIZATION));
        return userService.myGroupMemberList(userId);
    }

    @PostMapping("/update")
    @ApiOperation(value = "修改用户信息")
    public ApiResult<?> update(@Valid @RequestBody UserReqDto userReqDto) {
        return userService.update(userReqDto);
    }

    @GetMapping("/info")
    @ApiOperation(value = "详情")
    public ApiResult<?> userInfo(@RequestParam("userId") Long userId, HttpServletRequest request) {
        return userService.getUserInfo(userId);
    }

}
